import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Vector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class JointureTest {

	String R_file = "R5K.txt"; 
	String S_file = "S50K.txt";
	Vector<String> R;
	Vector<String> S;
	Vector<String> RS;
	
	Jointure jointure;
	
	@Before
	public void setUp() throws Exception {
		R = new Vector<String>();
		S = new Vector<String>();
		jointure = new Jointure();		
	}
	
	/*@Test
	public void testReadFromFileToRelation(){		
		
	}*/

	@Test
	public void testJoin() {
		readFromFileToRelation(R_file, R);
		readFromFileToRelation(S_file, S);
		RS = jointure.join(R,S);
		assertNotNull("RS: ",RS);
	}
	
//	@Test
	public void testJoin_50k_50k() {
		readFromFileToRelation("R50K.txt", R);
		readFromFileToRelation("S50K.txt", S);
		RS = jointure.join(R,S);
		assertNotNull("RS: ",RS);
	}
	
	@After
	public void tearDown() throws Exception{
		BufferedWriter outputFile = new BufferedWriter(new FileWriter(new File("RS.txt")));
		for(String str: RS){
			outputFile.write(str);			
			outputFile.write("\n");
		}
		outputFile.close();
		System.out.print("Done!");
	}
	
	static public void readFromFileToRelation(String fileName, Vector<String> relationName){
		try{
			
    		//read R.txt
		    DataInputStream in = new DataInputStream(new FileInputStream(fileName));
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
		    String strLine;
		    int i = 0;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null){		   
		    	relationName.add(strLine);
		    	i++;
		    }
		       	
		    //Close the input stream
		    in.close();
    		
    	}
    	catch(Exception e){
    		System.out.println(e);
    	}
	}

}
